package com.buba.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.annotation.MyLog;
import com.buba.config.service.MyUserDetailService;
import com.buba.pojo.LgUser;
import com.buba.pojo.LgUserRole;
import com.buba.service.LgUserRoleService;
import com.buba.service.LgUserService;
import com.guli.common.ov.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author MrJia
 * @since 2020-10-27
 */
@RestController
@RequestMapping("/lgUser")
public class LgUserController {

    @Autowired
    private MyUserDetailService myUserDetailService;
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private LgUserService lgUserService;
    @Autowired
    private LgUserRoleService lgUserRoleService;

    @MyLog(module = "用户表", operatingType = "登陆", desc = "登陆成功")
    @PostMapping("/login")
    public R login(@RequestBody LgUser lgUser) {
        try {
            authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(lgUser.getTelephone(), lgUser.getPassword()));
            return R.ok().data("lgInfo", myUserDetailService.getMap());
        } catch (BadCredentialsException e) {
            return R.error().message("用户名密码错误");
        }
    }

    @Transactional
    @RequestMapping("insertUser")
    public R insertUser(@RequestBody LgUser lgUser) {
        PasswordEncoder pass=new BCryptPasswordEncoder();
        String encode = pass.encode("123456");
        lgUser.setPassword(encode);
        if(StringUtils.isBlank(lgUser.getId())){
            boolean b = lgUserService.save(lgUser);
            if (b) {
                LgUserRole lgUserRole = new LgUserRole();
                lgUserRole.setUserId(lgUser.getId());
                lgUserRole.setRoleId(lgUser.getRoleId());
                boolean save = lgUserRoleService.save(lgUserRole);
                if (save) return R.ok().message("successful");
            }
            return R.error().message("error");
        }
        boolean b = lgUserService.updateById(lgUser);
        if (b) {
            LgUserRole lgUserRole = new LgUserRole();
            lgUserRole.setUserId(lgUser.getId());
            lgUserRole.setRoleId(lgUser.getRoleId());
            QueryWrapper<LgUserRole> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(LgUserRole::getUserId,lgUser.getId());
            boolean update = lgUserRoleService.update(lgUserRole, wrapper);
            if (update) return R.ok().message("successful");
        }
        return R.error().message("error");
    }

    @RequestMapping("selectListByDeptId")
    public R selectListByDeptId(@RequestBody LgUser lgUser) {
        return R.ok().data("UserDeptList", lgUserService.selectListByDeptId(lgUser));
    }

}

